package com.chanyachting.emall.action.companyproduct;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Companyproduct;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.domain.ServiceOptionType;
import com.chanyachting.emall.form.CompanyProductForm;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.service.ProductimageBO;
import com.chanyachting.emall.serviceImpl.MemberorderservicesBO;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.MyFile;
import com.chanyachting.emall.util.RequestUtil;


/**
 * 创建普通类型商品
 * @author robert
 *
 */
public class CreateNomalProductAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);

		try {
			
			CompanyProductForm cpform=(CompanyProductForm) form;
			
			Companyproduct cp=new Companyproduct();
			
			if(cpform.validate(mapping, request).isEmpty()){
				
				BeanUtils.copyProperties(cp, cpform);
				
				String imgUrl=MyFile.saveFileForm(cpform.getFile(), request);
				
				if(imgUrl!=null && !imgUrl.equals("")) cp.setImgUrl(imgUrl);
				
				cp.setCreateDate(DateUtil.getDate());  //设置创建时间
				
				cp.setUpdateDate(DateUtil.getDate());  //设置最后更新时间
				
				String optype=cpform.getOptype();
				
				//System.out.println("-- optype:"+optype);
				
				//待上架
				if(optype.equals("0")){
					
					cp.setStatus("00");
				}
				
				//发布展示
				if(optype.equals("1")){
					
					//检查会员是否有对应服务
					Member member=(Member)ru.getSessionObject(MemberService.MEMBER_IN_SESSION,new Member());
					
					String optionType=ServiceOptionType.Product_Shelves;
					
					boolean temp=MemberorderservicesBO.checkMmemberServiceByMemberAndOPType(member, optionType);
					
					if(temp){
						
						cp.setStatus("01");
					}else{
						
						ru.setMsg("发布展示失败！请确认已经购买/开启产品上架服务。");
						
						return mapping.findForward("create");
					}
				}
				
				
				if(cp.save()){
					
					//公司产品创建成功，处理产品图片
					String img1=MyFile.saveFileForm(cpform.getPimg1(), request);
					
					ProductimageBO.createProductimage(cp.getId(), img1, cpform.getImgdesc1());
					
					String img2=MyFile.saveFileForm(cpform.getPimg2(), request);
					
					ProductimageBO.createProductimage(cp.getId(), img2, cpform.getImgdesc2());
					
					String img3=MyFile.saveFileForm(cpform.getPimg3(), request);
					
					ProductimageBO.createProductimage(cp.getId(), img3, cpform.getImgdesc3());
					
					String img4=MyFile.saveFileForm(cpform.getPimg4(), request);
					
					ProductimageBO.createProductimage(cp.getId(), img4, cpform.getImgdesc4());
					
					
					String img5=MyFile.saveFileForm(cpform.getPimg5(), request);
					
					ProductimageBO.createProductimage(cp.getId(), img5, cpform.getImgdesc5());
					
					//产品图片处理 end
					
					ru.setSuccessMsg();
					
				}else{
					
					ru.setFailMsg();
				}
				
				
			}
		
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		
		return mapping.findForward("create");
	}
	
	

}
